<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-card style="margin-top:-2px;">
        <div slot="header" class="clearfix">
          <span style="color:#329dff;font-weight:bold;font-size:14px;">数据集信息</span>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="数据集名称" prop="name">
              <el-input v-model="form.name" @blur="getCode" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="数据集编码" prop="code">
              <el-input v-model="form.code" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据集类型" prop="type">
              <el-select v-model="form.type" placeholder="数据集类型" clearable filterable="" style="width: 100%" >
                <el-option v-for="dict in dict.type.sy_dataset_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="数据集描述" prop="describe">
              <el-input type="textarea" v-model="form.describe" :rows="6"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" :rows="6"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <center>
      <div slot="footer" class="dialog-footer" style="margin-top:20px;">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">重 置</el-button>
      </div>
    </center>
  </div>
</template>

<script>
import { getCode,editDataset } from "@/api/system/dataset";

export default {
  name: "NewDataset",
  dicts: ['sy_dataset_type'],
  data() {
    return {
      form: {},
      rules: {
        name: [
          { required: true, message: "数据集名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "数据集名称不能为空", trigger: "blur" }
        ],
        describe: [
          { required: true, message: "数据集名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "请选择数据集类型", trigger: "change" }
        ],
      },
    };
  },
  created() {
    this.reset();
  },
  mounted(){
    // this.drawLine();
  },
  methods: {
    getCode(){
      if(this.form.name.replace(/\s/g,"").length==0){
        this.form.name = null;
      }
      if(this.form.name!=null){
        if(this.form.name.replace(/\s/g,"").length>0){
          let nameStr = this.form.name.replace(/\s/g,"");
          if(nameStr!=null&&nameStr.length>0){
            this.form.name = nameStr;
            getCode({name:nameStr}).then(res =>{
              if(res.code===200){
                this.form.code = res.data.code;
              }else{
                this.$message.error(res.msg);
              }
            })
          }else{
            this.$message.error("请输入数据集名称");
            this.form.name = null;
            return false;
          }
        }
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            editDataset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$router.push({path:"/datamanager/dataupload"});
              this.reset();
            });
          } else {
            editDataset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$router.push({path:"/datamanager/dataupload"});
              this.reset();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        name: null,
        code: null,
        type: null,
        describe: null,
        status: null,
        path: null,
        projectIds: null,
        engineerIds: null,
        subEngineerIds: null,
        stationIds: null,
        roleIds: null,
        publishTime: null,
        publishId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
  }
};
</script>
